package cn.dgut.training.dto;

import cn.dgut.training.entity.activity.Carousel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

@Data
public class AdDTO extends BaseDTO {

    private Long id;
    private String name;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date startTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date endTime;

    public static QueryWrapper getWrapper(AdDTO adDTO) {

        QueryWrapper<Carousel> queryWrapper = new QueryWrapper();

        if (adDTO.getId() != null) {
            queryWrapper.eq("id", adDTO.getId());
        }

        if (adDTO.getStartTime() != null) {
            queryWrapper.ge("start_time", adDTO.getStartTime());
        }

        if (adDTO.getEndTime() != null) {
            queryWrapper.le("end_time", adDTO.getEndTime());
        }

        if (!StringUtils.isBlank(adDTO.getOrderBy())) {
            if ("desc".equals(adDTO.getSort())) {
                queryWrapper.orderByDesc(adDTO.getOrderBy());
            } else {
                queryWrapper.orderByAsc(adDTO.getOrderBy());
            }
        }
        return queryWrapper;
    }
}
